<!DOCTYPE html>
<html>
<head>
<title>修改密码</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles/login.css"/>
<script type="text/javascript" src="scripts/jquery.min.js">
</script>
<script type="text/javascript" src="scripts/cookie_util.js">
</script>
<script type="text/javascript" src="scripts/base64.js">
</script>
<script type="text/javascript">
	var userId = getCookie("userId");
	var last_password;
	var new_password;
	var final_password;
	$(function(){
		/*
		 * 获得焦点，清空提示
		 */
		$("#last_password").unbind("focus").focus(function(){
			$("#warning_1").children("span").text("");
			$("#warning_1").hide();
		});
		$("#new_password").unbind("focus").focus(function(){
			$("#warning_2").children("span").text("");
			$("#warning_2").hide();
		});
		$("#final_password").unbind("focus").focus(function(){
			$("#warning_3").children("span").text("");
			$("#warning_3").hide();
		});
		/*
		 * 失去焦点，非空判断
		 */
		$("#last_password").unbind("blur").blur(function(){
			last_password = $("#last_password").val().trim();
			if(!last_password){
				$("#warning_1").children("span").text("原密码为空");
				$("#warning_1").show();
			}
		});
		$("#new_password").unbind("blur").blur(function(){
			new_password = $("#new_password").val().trim();
			if(!new_password){
				$("#warning_2").children("span").text("新密码为空");
				$("#warning_2").show();
			}else if(new_password.length <4 || new_password.length > 10){
				$("#warning_2").children("span").text("长度为4-10位");
				$("#warning_2").show();
			}else if(final_password && final_password != new_password){
				$("#warning_3").children("span").text("密码不一致");
				$("#warning_3").show();
			}
		});
		$("#final_password").unbind("blur").blur(function(){
			final_password = $("#final_password").val().trim();
			if(!final_password){
				$("#warning_3").children("span").text("确认密码为空");
				$("#warning_3").show();
			}else if(final_password != new_password){
				$("#warning_3").children("span").text("密码不一致");
				$("#warning_3").show();
			}
		});
		$("#changePassword").unbind("click").click(function(){
			if(last_password && final_password == new_password && 
					new_password.length >=4 && new_password.length <= 10){
				$("#warning_1").children("span").text("");
				$("#warning_1").hide();
				if(!userId){
					alert("操作超时，请重新登录!");
					window.location.href = "log_in.html";
				}else{
					$.ajax({
						url:"http://localhost:8080/note/user/changePassword.do",
						data:{"userId":userId,"last_password":last_password,"new_password":new_password},
						type:"post",
						dataType:"json",
						success:function(result){
							if(result.status == 2){
								$("#warning_1").children("span").text(result.msg);
								$("#warning_1").show();
							}
							if(result.status == 0){
								alert(result.msg);
								window.location.href = "log_in.html";
							}
						},
						error:function(){
							alert("修改密码失败，请联系管理员!")
						},
						async:true
					});
				}
			}
		});
	});
</script>
</head>
	<body>
		<div class="global">
			<div class="sig sig_in" tabindex='4' id='zc'>
				<dl>
					<dt>
						<div class='header'>
							<h3>修改密码</h3>
						</div>
					</dt>
					<dt></dt>
					<dt>
						<div class='letter'>
							原密码:&nbsp;<input type="password" name="" id="last_password" tabindex='1'/>
							<div class='warning' id='warning_1'><span></span></div>
						</div>
					</dt>
					<dt>
						<div class='letter'>
							新密码:&nbsp;<input type="password" name="" id="new_password" tabindex='2'/>
							<div class='warning' id='warning_2'><span></span></div>
						</div>
					</dt>
					<dt>
						<div class='password'>
							确认新密码:&nbsp;<input type="password" name="" id="final_password" tabindex='3'/>
							<div class='warning' id='warning_3'><span></span></div>
						</div>
					</dt>
					<dt>
						<div>
							<input type="button" name="" id="changePassword" value='&nbsp;确&nbsp;定&nbsp;' tabindex='4'/>
							<input type="button" name="" id="back" value='&nbsp;关&nbsp;闭&nbsp;' onclick='javascript:history.go(-1);' tabindex='5'/>
							<script type="text/javascript">
							/*var flog = true;
							function get(e){
								return document.getElementById(e);
							}
							get('back').onclick=function(){
								get('zc').className='sig sig_out';
								//window.history.back();
								window.opener=null;
								window.open('','_self');
								//window.close();
							}
							window.onload=function(){
								get('last_password').onblur=function(){
									var lpassword=get('last_password').value;
									if(!validOldPwd(lpassword)){
										get('warning_1').style.display='block';
										flog = false;
									}else{
										get('warning_1').style.display='none';
										flog = true;
									}
								}
								
								get('final_password').onblur=function(){
									
									var npassword=get('new_password').value;
									var fpassword=get('final_password').value;
									if(npassword!=fpassword){
										get('warning_3').style.display='block';
										flog = false;
									}else{
										flog = true;
									}
								}
								get('new_password').onblur=function(){
									var npassword=get('new_password').value.length;
									if(npassword<6&&npassword>0){
										get('warning_2').style.display='block';
										flog = false;
									}else{
										flog = true;
									}
								}
								get('new_password').onfocus=function(){
									get('warning_2').style.display='none';
								}
								get('final_password').onfocus=function(){
									get('warning_3').style.display='none';
								}
								$("#changePassword").click(function(){
									if(flog){
										changepwd(changepwdSuccess,changepwdError);
									}
									
								})
								
							}*/
							</script>
						</div>
					</dt>
				</dl>
			</div>
		</div>
	</body>
</html>